/**
 * 
 */
package com.wskj.library.employer.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.wskj.library.pojo.Employer;
import com.wskj.library.pojo.RoleInfo;
import com.wskj.library.service.EmployerService;
import com.wskj.library.service.impl.EmployerServiceImpl;
import com.wskj.util.RequestUtil;
import com.wskj.util.StringUtil;

/**
 * @author 胡洋
 * @describe 
 * @Date 2018年3月22日下午6:11:24
 * @version 1.0
 */
@MultipartConfig
@WebServlet(urlPatterns="/employer/update.do")
public class UpdateEmployerServlet extends HttpServlet {
	EmployerService  emService=new EmployerServiceImpl(); 
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    int eid = StringUtil.nullToInt(req.getParameter("id"));
		Employer employer = emService.getEmployerById(eid);
		List<RoleInfo> roles = emService.queryAllRoles();
		req.setAttribute("roles", roles);		
		req.setAttribute("em",employer);
		req.getRequestDispatcher("/WEB-INF/page/employer/update.jsp").forward(req, resp);
	}
	
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		Employer employer = RequestUtil.convertRequestToEntity(Employer.class, req);
		Part part = req.getPart("img");	
		
		//上传文件的名称....
		String name= part.getSubmittedFileName();	
		if(!name.trim().isEmpty()) {
			//获取文件的后缀名
			String ext =name.substring(name.lastIndexOf(".")+1) ;	
			//判断后缀名是否合法  .....		
			String fileName = UUID.randomUUID().toString() + "."+ext;		
			part.write("D:/img/employer/" + fileName);
			// 文件地址: 头像的地址
			String filePath = "http://192.168.3.1:8080/img/employer/" + fileName;
			employer.setE_img(filePath);
		}
		
		boolean result=emService.updateEmployer(employer);
		
		resp.sendRedirect(req.getContextPath()+"/employer/list.do");
	}

}
